class Settings{
    constructor(root){
        this.root=root;
        this.platform="WEB";
        if(this.root.AcWingOS){
            this.platform="ACAPP";
        }
        this.username = "";
        this.photo = "";

        this.$settings = $(`
            <div class="ac-game-settings">
                <div class="ac-game-settings-login">
                    <div class="ac-game-settings-title">
                        登录
                    </div>
                    <div class="ac-game-settings-username">
                        <div class="ac-game-settings-item">
                            <input type="text" placeholder="用户名"></input>
                        </div>
                    </div>
                    <div class="ac-game-settings-password">
                        <div class="ac-game-settings-item">
                            <input type="password" placeholder="密码"></input>
                        </div>
                    </div>
                    <div class="ac-game-settings-submit">
                        <div class="ac-game-settings-item">
                            <button>登录</button>
                        </div>
                    </div>
                    <div class="ac-game-settings-error-messages">
                        
                    </div>
                    <div class="ac-game-settings-option">
                        注册
                    </div>
                    <br>
                    <div clas<div class="ac-game-settings-acwing">
                        <img src="https://app2825.acapp.acwing.com.cn/static/image/settings/acwing-logo.png"></img>
                        <br>
                        <div>
                        ACwing一键登录
                        </div>
                    </div>
                </div>
                <div class="ac-game-settings-register">
                    <div>
                    <div class="ac-game-settings-title">
                        注册
                    </div>
                    <div class="ac-game-settings-username">
                        <div class="ac-game-settings-item">
                            <input type="text" placeholder="用户名"></input>
                        </div>
                    </div>
                    <div class="ac-game-settings-password ac-game-settings-password-first">
                        <div class="ac-game-settings-item">
                            <input type="password" placeholder="密码"></input>
                        </div>
                    </div>
                     <div class="ac-game-settings-password ac-game-settings-password-second">
                        <div class="ac-game-settings-item">
                            <input type="password" placeholder="确认密码"></input>
                        </div>
                    </div>
                    
                    <div class="ac-game-settings-submit">
                        <div class="ac-game-settings-item">
                            <button>注册</button>
                        </div>
                    </div>
                    <div class="ac-game-settings-error-messages">
                    </div>
                    <div class="ac-game-settings-option">
                        登录
                    </div>
                    <br>
                    <div clas<div class="ac-game-settings-acwing">
                        <img src="https://app2825.acapp.acwing.com.cn/static/image/settings/acwing-logo.png"></img>
                        <br>
                        <div>
                        ACwing一键登录
                        </div>
                    </div>
                </div>
            </div>
       `)
        this.$login = this.$settings.find(`.ac-game-settings-login`);
        this.$login_username = this.$login.find(`.ac-game-settings-username input`);
        this.$login_password = this.$login.find(`.ac-game-settings-password input`);
        this.$login_submit = this.$login.find(`.ac-game-settings-submit button`);
        this.$login_error_messages = this.$login.find(`.ac-game-settings-error-messages`);
        this.$login_register = this.$login.find(`.ac-game-settings-option`);

        this.$login.hide();


        this.$register = this.$settings.find(`.ac-game-settings-register`);
        this.$register_username = this.$register.find(`.ac-game-settings-username input`);
        this.$register_password = this.$register.find(`.ac-game-settings-password-first input`);
        this.$register_password_confirm = this.$register.find(`.ac-game-settings-password-second input`);
        this.$register_submit = this.$register.find(`.ac-game-settings-submit button`);
        this.$register_error_messages = this.$register.find(`.ac-game-settings-error-messages`);
        this.$register_login = this.$register.find(`.ac-game-settings-option`);

        this.$acwing_login = this.$settings.find('.ac-game-settings-acwing img');

        this.$register.hide();
        this.root.$ac_game.append(this.$settings);

        this.start();
    }
    start(){//platform表示在网站端还是acapp端
        if(this.platform === "ACAPP"){
            this.getinfo_acapp();
        }
        else{
            this.getinfo_web();
            this.add_listening_events();
        }
    }

    add_listening_events(){
        let outer = this;
        this.add_listening_events_login();
        this.add_listening_events_register();
        this.$acwing_login.click(function(){
            outer.acwing_login();
        })
    }

    acwing_login(){
        $.ajax({
            url:"https://app2825.acapp.acwing.com.cn/settings/acwing/web/apply_code",
            type:"GET",
            success: function(resp){
                if(resp.result === "success"){
                    window.location.replace(resp.apply_code_url);
                }
            }
        })
    }
    add_listening_events_login(){
        let outer = this;
        this.$login_register.click(function(){
            outer.register();
        });
        this.$login_submit.click(function(){
            outer.login_on_remote();
        })
    }

    login_on_remote(){//在远程服务器上登录
        let outer = this;
        let username = this.$login_username[0].value;
        let password = this.$login_password[0].value;
        this.$login_error_messages.empty();
        $.ajax({
            url: "https://app2825.acapp.acwing.com.cn/settings/login/",
            type: "GET",
            data: {
                username : username,
                password : password,
            },
            success : function(resp){
                if(resp.result === "success"){
                    location.reload();
                }else{
                    outer.$login_error_messages.html(resp.result);
                }
            }
        })
    }
    logout_on_remote(){//在远程服务器上登出
        if (this.platform === "ACAPP") 
        {
            this.root.AcWingOS.api.window.close();
        }
        else
        {
        $.ajax({
            url: "//app2825.acapp.acwing.com.cn/settings/logout/",
            type: "GET",
            success : function(resp){
                location.reload();
            }
        })
        }
    }
    add_listening_events_register(){
        let outer = this;
        this.$register_login.click(function(){
            outer.login();
        })
        this.$register_submit.click(function(){
            outer.register_on_remote();
        })
    }
    register_on_remote(){//在远程服务器上注册
        let outer = this;
        let username = this.$register_username.val();
        let password = this.$register_password.val();
        let password_confirm = this.$register_password_confirm.val();
        this.$register_error_messages.empty();
        
        $.ajax({
            url: "https://app2825.acapp.acwing.com.cn/settings/register/",
            type: "GET",
            data: {
                username: username,
                password: password,
                password_confirm: password_confirm,
            },
            success: function(resp){
                if(resp.result === "success"){
                    location.reload();
                }else{
                    outer.$register_error_messages.html(resp.result);
                }
            }
        });


    }

    register(){//打开注册界面
        this.$login.hide();
        this.$register.show();
    }
    login(){//打开登录界面
        this.$register.hide();
        this.$login.show();
    }

    getinfo_web(){
        let outer = this;
        $.ajax({
            url: "https://app2825.acapp.acwing.com.cn/settings/getinfo/",
            type: "GET",
            data: {
                platform:outer.platform,
            },
            success: function(resp){
                if(resp.result === "success"){
                    outer.username = resp.username;
                    outer.photo = resp.photo;
                    outer.hide();
                    outer.root.menu.show();
                }else{
                    outer.login();
                }
            }
        });
    }
    getinfo_acapp(){
        let outer = this;
        $.ajax({
            url:"https://app2825.acapp.acwing.com.cn/settings/acwing/acapp/apply_code/",
            type:"GET",
            success : function(resp){
                if(resp.result === "success"){
                    outer.acapp_login(resp.appid, resp.redirect_uri, resp.scope, resp.state);
                }
            }
        })

    }
    acapp_login(appid, redirect_uri,scope,state){
        let outer = this;
        this.root.AcWingOS.api.oauth2.authorize(appid, redirect_uri, scope, state, function(resp){
            if(resp.result === "success"){
                outer.username = resp.username;
                outer.photo = resp.photo;
                outer.hide();
                outer.root.menu.show();
            }
        });
    }
    hide(){
        this.$settings.hide();
    }
    show(){
        this.$settings.show();
    }
}
